import request from "@/utils/request";
import Register from "@/components/Register.vue";
import {routes} from "vue-router/auto-routes";


export const userRegisterService = (registerData)=> {
    const params = new URLSearchParams();
    for(let key in registerData){
        params.append(key, registerData[key]);
    }
    return request.post('/register', params, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });
}

export const sendCaptchaService = async (email) => {
    const params = new URLSearchParams();
    params.append('email', email);
    return request.post('/sendCaptcha', params, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });
};

export const verifyCaptchaService = async (email, captcha) => {
    const params = new URLSearchParams();
    params.append('email', email);
    params.append('captcha', captcha);
    return request.post("/verifyCaptcha", params, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });
};


export const userLoginService = (loginData)=> {
    const params = new URLSearchParams();
    for(let key in loginData){
        params.append(key, loginData[key]);
    }
    //console.log(`Request body: ${params.toString()}`);
    return request.post('/login', params, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });
}

export const userChangeNameService = (username)=> {
    const params = new URLSearchParams();
    params.append('username', username);
    return request.patch('/user/modifyName', params, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });
}

export const userModifyPasswordService = (pwdData)=> {
    return request.patch('/user/modifyPwd', pwdData);
}

export const userInfoService = ()=> {
    return request.get('/user/userInfo');
}

export const userSelectAllService = ()=> {
    return request.get('/user/selectAll');
}

export const userChangeRoleService = (id, role)=> {
    const params = new URLSearchParams();
    params.append('id', id);
    params.append('role', role);
    return request.patch('/user/changeRole', params, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });
}

export const userDeleteService = (id)=> {
    const params = new URLSearchParams();
    params.append('id', id);
    return request.delete('/user/deleteUser', {
        params,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });
}

export function userUploadAvatarService(formData) {
    return request.post("/user/uploadAvatar", formData, {
        headers: {
            "Content-Type": "multipart/form-data"
        }
    });
}
